package com.kdtech.analyse.app;
import java.util.HashMap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.StringUtils;

/**
 * @author dhy
 * @name 21世纪
 * @link http://apps.21sq.org/herald/getList
 */
public class _21sqAppAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://app.21sq.org/iPaper/api/[0-9]*/herald_[0-9]*.json",
	};

	public static HashMap<String, String> getLinks(String url){
		HashMap<String, String> links = new HashMap<String, String>();
		
		try{
			String htmltxt = null;
			if(StringUtils.isNotBlank(htmltxt)){
				JSONObject jObj = JSONObject.parseObject(htmltxt);
				JSONArray newsList = JSONArray.parseArray(jObj.getString("list"));
				for(int i = 0; i < newsList.size(); i++){	
					try{
						String api = newsList.getJSONObject(i).getString("api");
						String link = "http://app.21sq.org/iPaper/api/" + api;
						links.put(link, "");
					}catch(Exception e){
						continue;
					}
				}
			}
		}catch(Exception e){
			
		}
		return links;
	}
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;

		JSONObject data = JSONObject.parseObject(htmltxt);
		title = data.getString("title");
		date = data.getLong("inputtime") * 1000;
		content = data.getString("content");
		author = data.getString("author");
		
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	

	

	
}

